<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/6
 * Time: 16:28
 */

class Apply_Model extends API_Model
{

    public function __construct()
    {
        parent::__construct();
    }

    // main function
    function run(Array $params)
    {
        $send['state'] = false;
        do {

            $userid = empty($params['userid']) ? $params['userId'] : $params['userid'];

            if ($userid == 0 || empty($params['name']) || empty($params['phone']) || empty($params['email']) ||
                empty($params['company']) || empty($params['license_url']) || empty($params['organization_url'])) {
                $send['error'] = '请填写完整';
                break;
            }

            if (empty($params['phone']) || !preg_match('/^1[34578]\d{9}$/', $params['phone'])) {
                $send['error'] = '手机号格式错误';
                break;
            }

            $posts = [
                'userid' => $userid,
                "name" => value($params, 'name'),
                "phone" => value($params, 'phone'),
                "email" => value($params, 'email'),
                "wechat" => value($params, 'wechat'),
                "company" => value($params, 'company'),
                "channel" => value($params, 'channel'),
                "license_url" => value($params, 'license_url'),
                "organization_url" => value($params, 'organization_url'),
                "address" => value($params, "address")
            ];

            $db = clone($this->db);
            $db->insert('purchaser_apply', $posts);
            if (!$db->insert_id()) {
                $send['error'] = '申请失败，请联系客服';
                break;
            }

            $send['state'] = true;
            $send['data'] = [
                "msg" => '申请成功'
            ];

        } while(false);
        $this->output->send($send);
    }

}